package ore.example.pokemoninfo.GUI;

import com.pixelmonmod.pixelmon.api.pokemon.Pokemon;
import com.pixelmonmod.pixelmon.api.storage.PartyStorage;
import com.pixelmonmod.pixelmon.api.storage.StorageProxy;
import com.pixelmonmod.pixelmon.api.util.helpers.SpriteItemHelper;
import de.tr7zw.nbtapi.NBTItem;
import ore.example.pokemoninfo.Pokemoninfo;
import ore.example.pokemoninfo.utils.CustomConfig;
import ore.example.pokemoninfo.utils.PokemonSlotItem;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_21_R1.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import xyz.xenondevs.invui.gui.PagedGui;
import xyz.xenondevs.invui.gui.structure.Markers;
import xyz.xenondevs.invui.item.Item;
import xyz.xenondevs.invui.item.builder.ItemBuilder;
import xyz.xenondevs.invui.item.impl.SimpleItem;
import xyz.xenondevs.invui.window.Window;

import java.util.ArrayList;
import java.util.List;

import static ore.example.pokemoninfo.utils.PlaceholderUtil.replacePlaceholders;

public class PokemonInfoGUI {

    public static void open(Player player) {
        PartyStorage storage = StorageProxy.getPartyNow(player.getUniqueId());
        if (storage == null) {
            player.sendMessage("§c无法获取你的宝可梦队伍信息！");
            return;
        }
        Pokemon[] pokemonList = storage.getAll();
        List<Item> pokemonItems = new ArrayList<>();

        CustomConfig config = Pokemoninfo.getPokemonPartyConfig();
        List<String> loreTemplate = config.getConfig().getStringList("lore");

        for (int i = 0; i < pokemonList.length; i++) {
            Pokemon pokemon = pokemonList[i];
            ItemStack displayStack;

            if (pokemon != null) {
                net.minecraft.world.item.ItemStack nmsStack = SpriteItemHelper.getPhoto(pokemon);
                displayStack = CraftItemStack.asBukkitCopy(nmsStack);

                String displayName = pokemon.getTranslatedName().getString();
                if (pokemon.isShiny()) displayName = "§e✦ " + displayName;

                ItemMeta meta = displayStack.getItemMeta();
                if (meta != null) {
                    meta.setDisplayName(displayName);
                    List<String> lore = new ArrayList<>();
                    for (String line : loreTemplate) {
                        lore.add(replacePlaceholders(line, pokemon).replace('&', '§'));
                    }
                    meta.setLore(lore);
                    displayStack.setItemMeta(meta);
                }

                NBTItem nbti = new NBTItem(displayStack);

                nbti.setInteger("party_index", i);
                displayStack = nbti.getItem();

                pokemonItems.add(new PokemonSlotItem(displayStack, i));
            } else {
                displayStack = new ItemStack(Material.BARRIER);
                ItemMeta meta = displayStack.getItemMeta();
                if (meta != null) {
                    meta.setDisplayName("§7空位");
                    displayStack.setItemMeta(meta);
                }
                pokemonItems.add(new SimpleItem(new ItemBuilder(displayStack)));
            }
        }

        Item border = new SimpleItem(new ItemBuilder(Material.GRAY_STAINED_GLASS_PANE).setDisplayName(" "));
        Item countItem = new SimpleItem(new ItemBuilder(Material.PAPER).setDisplayName("§b宝可梦队伍"));

        PagedGui<Item> gui = PagedGui.items()
                .setStructure(
                        "# # # # # # # # #",
                        "# # x # x # x # #",
                        "# # # # # # # # #",
                        "# # x # x # x # #",
                        "# # # # # # # # #",
                        "# # # < c > # # #"
                )
                .addIngredient('#', border)
                .addIngredient('x', Markers.CONTENT_LIST_SLOT_HORIZONTAL)
                .addIngredient('c', countItem)
                .setContent(pokemonItems)
                .build();

        Window window = Window.single()
                .setViewer(player)
                .setTitle("§a" + player.getName() + "的宝可梦队伍")
                .setGui(gui)
                .build();
        window.open();
    }
}
